<?php

namespace App\Http\Controllers\Backend;

use App\Criteria\PostSearchCriteria;
use App\Events\BlogView;
use App\Http\Controllers\AppBaseController;
use App\Http\Requests\CreatePostRequest;
use App\Http\Requests\UpdatePostRequest;
use App\Repositories\PostRepository;
use Flash;
use Illuminate\Http\Request;
use Response;

class PostController extends AppBaseController
{
    /** @var  PostRepository */
    private $postRepository;

    public function __construct(PostRepository $postRepo)
    {
        $this->postRepository = $postRepo;
//        $this->middleware('log')->only('index');
//        $this->middleware('subscribed')->except('store');
    }

    /**
     * Display a listing of the Post.
     *
     * @param Request $request
//     * @return Response
     */
    public function index(Request $request)
    {
        $this->postRepository->pushCriteria(new PostSearchCriteria($request));
//        Post::where('id',243)->
//        $this->postRepository->pushCriteria(new RequestCriteria($request));
//        \DB::table('posts')->where('id',243)->increment('view_count');
//        $res=\DB::table('posts')->where('id',243)->increment('view_count');
//        $this->postRepository->find(243)->increment('view_count',1);
//        Post::find(243)->increment('view_count');
//        dd($res);
        $posts = $this->postRepository->with('category')->paginate(10);
//        $postPresenter= app('presenter.post');
//        dd($postPresenter);

//        dd($posts);

//        $post=$this->postRepository->first()->published_at->diffForHumans();
//        dd($post);
//        $category_ids = \App\Models\Category::pluck('name','id');
//        dd($category_ids);

        return view('backend.posts.index')->with('posts', $posts);
    }

    /**
     * Show the form for creating a new Post.
     *
     * @return Response
     */
    public function create()
    {
        return view('backend.posts.create');
    }

    /**
     * Store a newly created Post in storage.
     *
     * @param CreatePostRequest $request
     *
     * @return Response
     */
    public function store(CreatePostRequest $request)
    {

//        $validator = \Validator::make($request->all(), [
//            'title' => 'required|unique:posts|max:255',
//            'body' => 'required',
//        ]);
//
//        if ($validator->fails()) {
//            return redirect('post/create')
//                ->withErrors($validator)
//                ->withInput();
//        }
        $input = $request->all();
        $post = $this->postRepository->create($input);
        Flash::success('Post saved successfully.');
        return redirect(route('admin.posts.index'));
    }
    /**
     * Display the specified Post.
     * @param  int $id
     *
//     * @return Response
     */
    public function show($id)
    {
        $post = $this->postRepository->findWithoutFail($id);
        // 获取当前文章
//        $current = Post::find($id);
        if (empty($post)) {
            Flash::error('Post not found');
            return redirect(route('admin.posts.index'));
        }
        //绑定blogview event
//      \Event::fire(new BlogView($post));
        event(new BlogView($post));
//        \Event::fire()

//        dd(\Event::getListeners('App\Events\BlogView'));
        return view('backend.posts.show',compact('post'));
//        return view('posts.show')->with('post', $post);
    }

    /**
     * Show the form for editing the specified Post.
     * @param  int $id
     * @return Response
     */
    public function edit($id)
    {
        $post = $this->postRepository->findWithoutFail($id);

        if (empty($post)) {
            Flash::error('Post not found');
            return redirect(route('admin.posts.index'));
        }

        return view('backend.posts.edit')->with('post', $post);
    }

    /**
     * Update the specified Post in storage.
     *
     * @param  int              $id
     * @param UpdatePostRequest $request
     *
     * @return Response
     */
    public function update($id, UpdatePostRequest $request)
    {
        $post = $this->postRepository->findWithoutFail($id);

        if (empty($post)) {
            Flash::error('Post not found');
            return redirect(route('admin.posts.index'));
        }

        $post = $this->postRepository->update($request->all(), $id);

        Flash::success('Post updated successfully.');

        return redirect(route('admin.posts.index'));
    }

    /**
     * Remove the specified Post from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $post = $this->postRepository->findWithoutFail($id);
        if (empty($post)) {
            Flash::error('Post not found');
            return redirect(route('admin.posts.index'));
        }
        $this->postRepository->delete($id);
        Flash::success('Post deleted successfully.');
        return redirect(route('admin.posts.index'));
    }


    public function batch(Request $request){
        $op=$request->get('op');
        $ids=$request->get('ids');
    }
}
